var ctrPath = '/Public/Admin/asset/js/ctr/';
var tplPath = '/Public/Admin/tpls/';
/*
 oc.lazyLoad   通过 ocLazyLoad 实现动态（懒）加载模块和依赖
 */
var moveStroe = angular.module( 'moveStore', [ 'ui.router', 'ngCookies', 'oc.lazyLoad', 'ngFileUpload', 'ui.bootstrap', 'ng.ueditor', 'ngSanitize' ] );
// console.log(moveStroe);
// 把依赖的脚本进行注入操作
moveStroe.config( function( $provide, $compileProvider, $controllerProvider, $filterProvider ){
  moveStroe.controller = $controllerProvider.register;
  moveStroe.directive = $compileProvider.directive;
  moveStroe.filter = $filterProvider.register;
  moveStroe.factory = $provide.factory;
  moveStroe.service = $provide.service;
  moveStroe.constant = $provide.constant; //constant 定义全局变量
  moveStroe.constant( 'Modules_Config', [ {} ] );
  moveStroe.config( [ "$ocLazyLoadProvider", "Modules_Config", routeFn ] );
  function routeFn( $ocLazyLoadProvider, Modules_Config ){
    $ocLazyLoadProvider.config( {
      debug: false,
      events: false,
      modules: Modules_Config
    } );
  }
} )
// 注册器加载完成会执行一run方法
/**
 $state 是当前路由信息
 $stateParams 是当前路由参数 '/{bookType:[0-9]{1,4}}',
 **/
moveStroe.run( function( $templateCache, $rootScope, $state, $stateParams ){  //把模板缓存起来
  $rootScope.$state = $state;
  $rootScope.$stateParams = $stateParams;
} );
//公共模板控制器 如alert 弹框
moveStroe.controller( 'ModalInstanceCtrl', function( $uibModalInstance, data ){
  var $ctrl = this;
  $ctrl.data = data;
  $ctrl.ok = function(){
    $uibModalInstance.close();
  };
  $ctrl.cancel = function(){
    $uibModalInstance.dismiss( 'cancel' );
  };
} )
/**
 * 文件查看编辑
 */
moveStroe.controller( 'view_editUploadCtrl', function( $uibModalInstance, $http, data,myAlert){
  var $ctrl = this;
  $ctrl.data = data;
  $ctrl.ok = function(){
    var form={text:$ctrl.data.data,file:$ctrl.data.curPath+'/'+$ctrl.data.fileName};
    $http.post('/File/Home/save',form,{
      headers: { 'Content-Type': 'application/json' },
    })
      .success(function(data){
         if(data.status){
           myAlert.open(data.msg);
         }else{
           myAlert.open(data.msg);
         }
      }).error(function(data){
      myAlert.open('网络错误！');
    });
    $uibModalInstance.close();
  };
  $ctrl.cancel = function(){
    $uibModalInstance.dismiss( 'cancel' );
  };
} )
/**
 * 拖拽文件上传
 */
moveStroe.controller( 'FileUploadCtrl', function( $uibModalInstance, $rootScope, $cookieStore, $scope, $window, $http, data, myAlert ){
  var $ctrl = this;
  $ctrl.data = data;
  $scope.$broadcast( 'uploadStatus', true );
  $ctrl.ok = function(){
    var file = $ctrl.data.fileData;
    var filePath = $ctrl.data.filePath;
    var form = new FormData();
    form.append( 'file', file );
    form.append( 'filePath', filePath );
    $http.post( '/File/File/upload', form, {
      headers: { 'Content-Type': undefined },
    } ).success( function( data ){
      if( data.status ){
        $window.location.reload(); //从新刷新页面
        $cookieStore.put( "path_url", filePath ); //保存路径
      }else{
        myAlert.open( data.msg );
      }
    } ).error( function( data ){
      myAlert.open( data );
    } );
    $uibModalInstance.close();
  };
  $ctrl.cancel = function(){
    $uibModalInstance.dismiss( 'cancel' );
  };
} )
//加载js插件
moveStroe.load = function( ctr ){
  return [ "$ocLazyLoad", function( $ocLazyLoad ){
    return $ocLazyLoad.load( ctrPath + ctr );
  } ]
}
moveStroe.config( function( $stateProvider, $urlRouterProvider ){
  $urlRouterProvider.otherwise( '/index' );
  $stateProvider
    .state( 'login', {
      url: '/login',
      views: {
        '': {
          templateUrl: tplPath + 'login.html',
          resolve: {
            deps: moveStroe.load( 'loginCtr.js' ),
          }
        }
      }
    } )
    .state( 'index', {
      url: '/index',
      views: {
        '': {
          // templateUrl:tplPath+'index.html',
          templateUrl: '/Admin/index/main',
        },
        'main@index': {
          // templateUrl:tplPath+'main.html',
          templateUrl: '/Admin/index/content',
        },
      }
    } )
    .state( 'index.move', {
      url: '/move/:id',
      views: {
        'main@index': {
          templateUrl: '/Admin/Move/list',
          resolve: {
            deps: moveStroe.load( 'move/moveCtr.js' ),
          }
        }
      }
    } )
    .state( 'index.move.add', {
      url: '/add',
      views: {
        'main@index': {
          templateUrl: '/Admin/Move/add',
          resolve: {
            deps: moveStroe.load( 'move/moveCtr.js' ),
          }
        }
      }
    } ).state( 'index.move.edit', {
    url: '/edit/:aid',
    views: {
      'main@index': {
        templateUrl: '/Admin/Move/edit',
        resolve: {
          deps: moveStroe.load( 'move/moveCtr.js' ),
        }
      }
    }
  } )
    .state( 'index.class', {
      url: '/class',
      views: {
        'main@index': {
          templateUrl: '/Admin/Category/index',
          resolve: {
            deps: moveStroe.load( 'category/categoryCtr.js' ),
          }
        }
      }
    } )
    .state( 'index.class.add', {
      url: '/add',
      views: {
        'main@index': {
          templateUrl: '/Admin/Category/add',
          resolve: {
            deps: moveStroe.load( 'category/categoryCtr.js' ),
          }
        }
      }
    } )
    .state( 'index.class.edit', {
      url: '/edit/:id',
      views: {
        'main@index': {
          templateUrl: '/Admin/Category/edit',
          resolve: {
            deps: moveStroe.load( 'category/categoryCtr.js' ),
          }
        }
      }
    } ).state( 'index.file', {
    url: '/file',
    views: {
      'main@index': {
        templateUrl: '/File/Home/index',
        resolve: {
          deps: moveStroe.load( 'file/fileCtr.js' ),
        }
      }
    }
  } )
} )